package com.stars.services.impl.system;
import com.stars.common.base.ResultMsg;
import com.stars.common.file.FileUtils;
import com.stars.entity.dto.system.SysJsItemDTO;
import com.stars.entity.system.SysJsItemEntity;
import com.stars.services.system.SysJsItemConfigServices;
import com.stars.dao.mapper.system.SysJsItemMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.stars.services.impl.BaseConfigServiceImpl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title: 前端组件 业务逻辑
 * @author dj
 * @date 2019-10-14 20:33:38
 * @version V1.0
 *
 */
@Service
public class SysJsItemConfigServicesImpl  extends BaseConfigServiceImpl<SysJsItemMapper , SysJsItemEntity> implements SysJsItemConfigServices {

    @Autowired
    private SysJsItemMapper sysJsItemMapper;
    @Override
    public ResultMsg addItem(String path) throws Exception {
        if(StringUtils.isBlank(path)){
            return ResultMsg.failure("没有填写地址");
        }
        List<SysJsItemEntity> sysJsItemEntityList = readToJsXType(path);
        for (SysJsItemEntity sysJsItemEntity : sysJsItemEntityList) {
            List<SysJsItemDTO> list = sysJsItemMapper.querySysJsItemList(sysJsItemEntity.getName());
            //存在，跳过
            if(list !=null && list.size()>0){
                continue;
            }
            insert(sysJsItemEntity);
        }
        return ResultMsg.success();
    }

    /**
     * 获取js 组件
     * @param path js 文件路径
     * @return
     * @throws IOException
     */
    public static List<SysJsItemEntity> readToJsXType(String path) throws IOException {
        List<File> files = FileUtils.getFiles(path);
        List<SysJsItemEntity> sysJsItemEntityList = new ArrayList<>();
        for (File file : files) {
            //File file = new File(files.get(0).getPath());
            BufferedReader br = new BufferedReader(new FileReader(file));
            String str = null;
            SysJsItemEntity sysJsItemEntity = new SysJsItemEntity();
            String filePath = file.getPath();
            String [] srcPath = filePath.split("stars-web-ext");
            if(srcPath.length == 2){
                sysJsItemEntity.setPath(srcPath[1]);
            }else {
                sysJsItemEntity.setPath(srcPath[0]);
            }

            while ((str = br.readLine()) != null) {
                //包含 Ext.define(' 情况
                if(str.trim().indexOf("Ext.define(") ==0){
                    sysJsItemEntity.setClassName(replaceJsStr(str));
                }
                if(str.trim().indexOf("alias") ==0 || str.trim().indexOf("xtype") ==0){
                    String [] xTypeList = str.split(":");
                    if(!replaceJsStr(xTypeList[1]).contains(".")){
                        sysJsItemEntity.setName(replaceJsStr(xTypeList[1]));
                    }
                    break;
                }
            }
            if(StringUtils.isNotBlank(sysJsItemEntity.getName())){
                sysJsItemEntityList.add(sysJsItemEntity);
            }
        }
        return sysJsItemEntityList;
    }

    /**
     * 包含以下字符去空
     * @param str
     * @return
     */
    private static String replaceJsStr(String str){
        return str.replaceAll("Ext.define","")
                .replaceAll("'","")
                .replaceAll(",","")
                .replaceAll("Ext.define","")
                .replaceAll("\\\"","")
                .replaceAll("\\(","")
                .replaceAll("\\{","")
                .replaceAll("widget.","")
                .replaceAll(" ","").trim();

    }
}